use SAS
GO

 IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'InsUpdActivityDetails')
	BEGIN
		DROP  Procedure  dbo.InsUpdActivityDetails
	END
GO

CREATE Procedure [dbo].[InsUpdActivityDetails] (
	@ACTIVITYID				VARCHAR(10)
,	@ACTIVITYGUID			VARCHAR(100)	
,	@ACTIVITYNAME			VARCHAR(200)
,	@ACTIVITYTYPEID			VARCHAR(10)
,	@ACTIVITYSTATUSID		VARCHAR(10)
,	@STARTDATE				VARCHAR(15)	= ''
,	@STARTTIME				VARCHAR(15) = ''
,	@ENDDATE				VARCHAR(15)	= ''
,	@ENDTIME				VARCHAR(15) = ''
,	@NOTEID					VARCHAR(10)	 = ''
,	@NOTES					VARCHAR(max) = ''
,	@USERID					VARCHAR(10)	 = '1'
-- BELOW FIELDS GOES INTO TR_RELATIONSHIP TABLE
,	@ACCOUNTNO				VARCHAR(10)	 = '0'
,	@OWNERID				VARCHAR(10)	 = '1'
,	@PRIMARYCONTACTID		VARCHAR(10)	 = ''
)
AS
BEGIN

	-- ***********************************************
	-- BELOW PARAMETERS ARE CURRENTLY HARDCODED
	-- START: DONOT MODIFY THE BELOW CODE
	-- ***********************************************
	DECLARE
		@NOTETYPE				VARCHAR(10)

	SELECT
		@NOTETYPE				= '3'
	-- ***********************************************
	-- END: DONOT MODIFY THE BELOW CODE
	-- ***********************************************

	IF(ISNULL(@STARTDATE,'')='')	SET @STARTDATE	= CONVERT(VARCHAR(10), GetDate(), 101)
	IF(ISNULL(@STARTTIME,'')='')	SET @STARTTIME = CONVERT(VARCHAR(10), GetDate(), 108)
	IF(ISNULL(@ENDDATE,'')='')		SET @ENDDATE = CONVERT(VARCHAR(10), GetDate(), 101)
	IF(ISNULL(@ENDTIME,'')='')		SET @ENDTIME = CONVERT(VARCHAR(10), GetDate(), 108)



	BEGIN TRAN
		BEGIN TRY
			IF EXISTS(SELECT 1 FROM TR_ACTIVITYTASK WHERE ActivityTaskGUID = @ACTIVITYGUID) 
			BEGIN		-- UPDATE
				UPDATE TR_ACTIVITYTASK
				SET	[Description]		= @ACTIVITYNAME
				,	ActivityType		= @ACTIVITYTYPEID
				,	[Status]			= @ACTIVITYSTATUSID
				,	StartDate			= @STARTDATE + ' ' + @STARTTIME
				,	EndDate				= @ENDDATE + ' ' + @ENDTIME
				WHERE
					ACTIVITYTASKID		= @ACTIVITYID
				AND	ACTIVITYTASKGUID	= @ACTIVITYGUID
			END
			ELSE
			BEGIN		-- INSERT
				SELECT @ACTIVITYID		= ISNULL(MAX(ACTIVITYTASKID), 0) +1 FROM TR_ACTIVITYTASK

				INSERT INTO TR_ACTIVITYTASK (ActivityTaskId, ActivityTaskGuid, [Description],
											ActivityType, [Status], StartDate, EndDate, IsTask)
				VALUES(
					@ACTIVITYID			--	ActivityTaskId
				,	@ACTIVITYGUID		--	ActivityTaskGuid
				,	@ACTIVITYNAME		--	Description
				,	@ACTIVITYTYPEID		--	ActivityType
				,	@ACTIVITYSTATUSID	--	Status
				,	CAST(@STARTDATE + ' ' + @STARTTIME AS DATETIME)				--	StartDate
				,	CAST(@ENDDATE + ' ' + @ENDTIME AS DATETIME)				--	EndDate
				,	0					-- ISTASK
				)
			END

			exec InsUpdNotes
				@NOTEID				= @NOTEID
			,	@NOTESOURCEGUID		= @ACTIVITYGUID
			,	@SOURCEID			= @ACTIVITYID
			,	@NOTE				= @NOTES
			,	@USERID				= @USERID
			,	@NOTETYPE			= @NOTETYPE


			IF ( CAST(ISNULL(@ACCOUNTNO,'0') AS INT) > 0)
			BEGIN
				exec InsUpdRelationships
					@SOURCEGUID	= @ACTIVITYGUID
				,	@PARTNERID	= @ACCOUNTNO
				,	@RELATIONSHIPTYPE = 9 -- ACTIVITY PARTNER
				,	@ISMAIN		= 1
				,	@USERID		= @USERID
			END

			IF ( CAST(ISNULL(@OWNERID,'0') AS INT) > 0)
			BEGIN
				exec InsUpdRelationships
					@SOURCEGUID	= @ACTIVITYGUID
				,	@PARTNERID	= @OWNERID
				,	@RELATIONSHIPTYPE = 3 
				,	@ISMAIN		= 1
				,	@USERID		= @USERID
			END

			IF ( CAST(ISNULL(@PRIMARYCONTACTID,'0') AS INT ) > 0)
			BEGIN
				exec InsUpdRelationships
					@SOURCEGUID	= @ACTIVITYGUID
				,	@PARTNERID	= @PRIMARYCONTACTID
				,	@RELATIONSHIPTYPE = 2 
				,	@ISMAIN		= 1
				,	@USERID		= @USERID
			END

			COMMIT TRAN

			SELECT 
				'<DATA>
					<RESPONSE>
						<RESULT>S</RESULT>
						<MESSAGE>Activity Details saved successfully</MESSAGE>
					</RESPONSE>
				</DATA>' AS Result
		END TRY
		BEGIN CATCH
			ROLLBACK TRAN
			SELECT 
				'<DATA>
					<RESPONSE>
						<RESULT>E</RESULT>
						<MESSAGE><![CDATA[Unable to save Activity Details. '+ERROR_MESSAGE()+']]></MESSAGE>
					</RESPONSE>
				</DATA>' AS Result
		END CATCH
END
GO
